package com.one.rope.mvp.web.mybatis;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Weijian.liu
 * @desc Mybatis-plus代码生成器
 * @date 2020/02/19
 */

public class MybatisPlusGenerator {

  static final String dataSourceUrl = "jdbc:mysql://123.57.182.202:3306/one_rope_mvp?serverTimezone=UTC&useUnicode=true&useSSL=false&characterEncoding=utf8";
  static final String driverName = "com.mysql.cj.jdbc.Driver";
  static final String userName = "root";
  static final String password = "duozhu2020";
  static final String author = "wesley";

  public static void main(String[] args) {
    // 代码生成器
    AutoGenerator mpg = new AutoGenerator();

    // 全局配置
    GlobalConfig gc = new GlobalConfig();
    String projectPath = System.getProperty("userinfo.dir");
    gc.setOutputDir(projectPath + "/src/main/java");
    gc.setAuthor(author);
    gc.setOpen(false);
    gc.setFileOverride(true);
    gc.setActiveRecord(true);
    gc.setEnableCache(false);// XML 二级缓存
    gc.setBaseResultMap(true);// XML ResultMap
    gc.setBaseColumnList(false);// XML columList
    gc.setDateType(DateType.ONLY_DATE);
    mpg.setGlobalConfig(gc);

    // 数据源配置
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setDbType(DbType.MYSQL);
    dsc.setUrl(dataSourceUrl);
    dsc.setDriverName(driverName);
    dsc.setUsername(userName);
    dsc.setPassword(password);
    dsc.setTypeConvert(new MySqlTypeConvert() {
      // 自定义数据库表字段类型转换【可选】
      public IColumnType processTypeConvert(String fieldType) {
        System.out.println("转换类型：" + fieldType);
        // 注意！！processTypeConvert 存在默认类型转换，如果不是你要的效果请自定义返回、非如下直接返回。
        return super.processTypeConvert(gc, fieldType);
      }
    });
    mpg.setDataSource(dsc);

    // 策略配置
    StrategyConfig strategy = new StrategyConfig();
    strategy.setNaming(NamingStrategy.underline_to_camel);
    strategy.setColumnNaming(NamingStrategy.underline_to_camel);
    strategy.setInclude(new String[]{"com_system_config"}); // 需要生成的表
    // strategy.setExclude(new String[]{"test"}); // 排除生成的表
        strategy.setTablePrefix(new String[]{"com_"});  //表名前缀，设置后生成的名字会去掉
    mpg.setStrategy(strategy);

    // 包配置
    PackageConfig pc = new PackageConfig();
    pc.setParent("com.one.rope.mvp.web");
    pc.setModuleName("business");
    pc.setEntity("entity");
    pc.setMapper("dao");
    pc.setXml(null);
    pc.setService("service");
    pc.setServiceImpl("service.impl");
    mpg.setPackageInfo(pc);

    // 自定义配置
    InjectionConfig cfg = new InjectionConfig() {
      @Override
      public void initMap() {
        // to do nothing
      }
    };
    List<FileOutConfig> focList = new ArrayList<FileOutConfig>();
    focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
      @Override
      public String outputFile(TableInfo tableInfo) {
        // 自定义输入文件名称
        return projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper"
            + StringPool.DOT_XML;
      }
    });
    cfg.setFileOutConfigList(focList);
    mpg.setCfg(cfg);
    mpg.setTemplate(new TemplateConfig().setXml(null).setController(null));

    mpg.setTemplateEngine(new FreemarkerTemplateEngine());
    mpg.execute();
  }
}
